package com.u.social.recommend.service.articles;

import com.alibaba.fastjson.JSON;
import com.u.social.recommend.model.ArticleDTO;
import com.u.social.recommend.model.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: u-social
 * @description: 推荐逻辑
 * @author: Alex Wu
 * @createDate: 2025-04-07 11:43
 **/
public class ArticlesRecommendService {

    @Autowired
    private ArticlesRecallService articlesRecallService;

    @Autowired
    private AIApiService aiApiService;

    @Autowired
    private RoughRankingService roughRankingService;

    @Autowired
    private FineRankingService fineRankingService;

    @Autowired
    private ReRankingService reRankingService;

    public List<ArticleDTO> recommendArticlesForNewUser() {

        // step1 召回
        Result result = recallInPool();

        // step2 粗排
        List<ArticleDTO> roughed = roughRankingService.roughSortByWeightRule(result.articleDTOS(), result.embeddingRecall(), result.exploreRecall());

        List<Long> roughedIds = roughed.stream().map(ArticleDTO::getId).toList();

        // step3 精排
        List<ArticleDTO> elaborateSort = fineRankingService.elaborateSort(roughedIds, result.userId());

        // step4 重排
        List<ArticleDTO> reRankArticles = reRankingService.reRankByActivityType(elaborateSort);

        return reRankArticles;
    }

    private Result recallInPool() {
        Long userId = 123L;
        UserDTO userDTO = new UserDTO();
        userDTO.setId(userId);
        userDTO.setName("lisi");
        userDTO.setAge(25);
        userDTO.setSex(1);
        userDTO.setPhone("Iphone 14 Pro Max");
        userDTO.setLikes("篮球,足球");

        // 加入向量扰动，加入随机性
        List<String> embedding = aiApiService.transformEmbedding(JSON.toJSONString(userDTO),true);

        // 召回1000条数据
        List<ArticleDTO> articleDTOS = articlesRecallService.hotRecall(400L);
        List<ArticleDTO> embeddingRecall = articlesRecallService.embeddingRecall(embedding, 500L);
        List<ArticleDTO> exploreRecall = articlesRecallService.exploreRecall(100L);
        Result result = new Result(userId, articleDTOS, embeddingRecall, exploreRecall);
        return result;
    }

    private record Result(Long userId, List<ArticleDTO> articleDTOS, List<ArticleDTO> embeddingRecall, List<ArticleDTO> exploreRecall) {
    }

    /**
     *  去重
     * @param articleDTOS
     * @return
     */
    public List<ArticleDTO> removeMultiple(List<ArticleDTO> articleDTOS) {
        List<ArticleDTO> result = new ArrayList<>();
        for (ArticleDTO articleDTO : articleDTOS) {
            if (!result.contains(articleDTO)) {
                result.add(articleDTO);
            }
        }
        return result;
    }

    /**
     *  去除已读
     * @param articleDTOS
     * @param userId
     * @return
     */
    public List<ArticleDTO> removeSeen(List<ArticleDTO> articleDTOS, Long userId) {
        System.out.println("从 redis 获取已经查看过的文章列表");
        List<ArticleDTO> result = new ArrayList<>();
        for (ArticleDTO articleDTO : articleDTOS) {
            if (!result.contains(articleDTO)) {
                result.add(articleDTO);
            }
        }
        return result;
    }







}
